<?php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

class FangownerRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        //调用自定义验证方法
        $this->checkPhone();
        return [
            'name'=>'required',
            'phone'=>'required|phone'
        ];
    }
    public function messages()
    {
        return [
            'name.required'=>'房东姓名不能为空'
        ];
    }
    //创建一个自定义的验证规则
    public function checkPhone(){
        //...$params 参数收集，将传递的参数以索引数组形式接收
        \Validator::extend('phone',function (...$params){
            $preg='/^1[1-9]\d{9}$/';
            return preg_match($preg,$params[1]);
        },'电话号码不合法');
    }
}
